package cn.com.controller;

import cn.com.beans.News;
import cn.com.service.impl.NewsServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsContorller {

    @Resource
    private NewsServiceImpl newsService;

    @GetMapping
    public String getAllNews(Integer type,ModelMap model, Integer cpage){

        if(type == 1){
            List<News> news = this.newsService.findAllNews();
            model.addAttribute("sum",news.size());
            model.addAttribute("cpage",cpage);
            model.addAttribute("news",news);
            int lastpage = news.size()/4+1;
            model.addAttribute("lastpage", lastpage);
            return "news/1";
        }else if(type == 2){
            List<News> news = this.newsService.findNewsByType(2);
            model.addAttribute("sum",news.size());
            model.addAttribute("cpage",cpage);
            model.addAttribute("news",news);
            int lastpage = news.size()/4+1;
            model.addAttribute("lastpage", lastpage);
            return "news/2";
        }else if(type == 3){
            List<News> news = this.newsService.findNewsByType(3);
            model.addAttribute("sum",news.size());
            model.addAttribute("cpage",cpage);
            model.addAttribute("news",news);
            int lastpage = news.size()/4+1;
            model.addAttribute("lastpage", lastpage);
            return "news/3";
        }else{
            List<News> news = this.newsService.findNewsByType(4);
            model.addAttribute("sum",news.size());
            model.addAttribute("cpage",cpage);
            model.addAttribute("news",news);
            int lastpage = news.size()/4+1;
            model.addAttribute("lastpage", lastpage);
            return "news/4";
        }
    }

    @GetMapping("/findnew")
    public String getNew(Integer id,ModelMap model){
        News news = this.newsService.findNewById(id);
        model.addAttribute("news",news);
        return "news/new";
    }


}
